﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Content.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the Content class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;

    /// <summary>
    /// Defines the Content class.
    /// </summary>
    public class Content
    {
        #region Fields
        /// <summary>
        /// The identity of the database table's row where this content object is saved.
        /// </summary>
        private int contentID;

        /// <summary>
        /// Stores the content's associated page's ID.
        /// </summary>
        private int pageID;

        /// <summary>
        /// Stores the content's author's ID.
        /// </summary>
        private int authorID;

        /// <summary>
        /// Stores the content's date and time it was created.
        /// </summary>
        private DateTime created;
        
        /// <summary>
        /// Stores the content's actual content.
        /// </summary>
        private string content;

        /// <summary>
        /// Stores the content's HPage associated page.
        /// </summary>
        private PageLayout page;

        /// <summary>
        /// Stores the content's HUser associated user.
        /// </summary>
        private Profile author;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the Content class.
        /// </summary>
        public Content()
        {
        }

        /// <summary>
        /// Initializes a new instance of the Content class.
        /// </summary>
        /// <param name="contentId">The identity of the database table's row referencing this content.</param>
        public Content(int contentId)
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetContentById", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ContentID", contentId);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            this.contentID = contentId;
                            this.pageID = Convert.ToInt32(rdr["PageID"], CultureInfo.InvariantCulture);
                            this.authorID = Convert.ToInt32(rdr["UserID"], CultureInfo.InvariantCulture);
                            this.created = Convert.ToDateTime(rdr["TimeStamp"], CultureInfo.InvariantCulture);
                            this.content = rdr["Content"] != DBNull.Value ? rdr["Content"].ToString() : string.Empty;
                        }
                        else
                        {
                            throw new DataException("Content does not exist.");
                        }
                    }
                }
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the content's database row identity.
        /// </summary>
        public int ContentId
        {
            get
            {
                return this.contentID;
            }
        }

        /// <summary>
        /// Gets or sets the content's associated page.
        /// </summary>
        public PageLayout Page
        {
            get
            {
                if (this.page == null && this.pageID != 0)
                {
                    this.page = new PageLayout(this.pageID);
                }
                
                return this.page;
            }

            set
            {
                this.page = value;
                this.pageID = value.PageId;
            }
        }

        /// <summary>
        /// Gets or sets the content's author.
        /// </summary>
        public Profile Author
        {
            get
            {
                if (this.author == null && this.authorID != 0)
                {
                    this.author = new Profile(this.authorID);
                }

                return this.author;
            }

            set
            {
                this.author = value;
                this.authorID = value.UserId;
            }
        }

        /// <summary>
        /// Gets the content's date and time it was created.
        /// </summary>
        public DateTime Created
        {
            get
            {
                return this.created;
            }
        }

        /// <summary>
        /// Gets or sets the content's html.
        /// </summary>
        public string Html
        {
            get
            {
                return this.content;
            }

            set
            {
                this.content = value;
            }
        }
        #endregion

        #region Methods
        
        #endregion
    }
}